//
//  XMRegistOneViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/22.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMRegistOneViewController.h"
#import <BRPickerView/BRPickerView.h>
#import "XMRegistTwoViewController.h"

@interface XMRegistOneViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *manBtn;
@property (weak, nonatomic) IBOutlet UIButton *wmanBtn;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UILabel *ageLa;
@property (weak, nonatomic) IBOutlet UILabel *addLa;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UIView *bgV;
@property (weak, nonatomic) IBOutlet UIView *codeBgV;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UIButton *randBtn;
@property (nonatomic, strong) NSMutableArray *ageArray;

#pragma mark - 参数
// 1男 2女
@property (nonatomic, copy) NSString *gender;

@end

@implementation XMRegistOneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gender = @"1";
    self.navView.backgroundColor = kHEXA(@"ffffff", 0);
    self.view.backgroundColor = kHEX(@"#F2F2F2");
    [self.navRightBtn setTitle:@"1/3" forState:UIControlStateNormal];
    [self.navRightBtn setTitleColor:kAppMainColor forState:UIControlStateNormal];
    
    kCornerRadius(_bgV, 10, 0, UIColor.clearColor);
    kCornerRadius(_codeBgV, 10, 0, UIColor.clearColor);
    kCornerRadius(_nextBtn, 21, 0, UIColor.clearColor);
    [_randBtn setImagePosition:XMImagePositionLeft spacing:6];
    
    [self.nameField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.codeField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.nameField.delegate = self;
    self.codeField.delegate = self;
    self.manBtn.selected = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:NO];
}

- (IBAction)manAct:(UIButton *)sender {
    self.manBtn.selected = YES;
    self.wmanBtn.selected = NO;
    [self.view endEditing:YES];
    self.gender = @"1";
}

- (IBAction)wmanAct:(UIButton *)sender {
    self.manBtn.selected = NO;
    self.wmanBtn.selected = YES;
    [self.view endEditing:YES];
    self.gender = @"2";
}

- (IBAction)randAct:(UIButton *)sender {
    [self.view endEditing:YES];
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{}.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/auth/obtainNickname" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            if (![XMUtil isBlankString:[result toString:@"nickname"]]) {
                self.nameField.text = [result toString:@"nickname"];
            }
        }
    }];

}

- (IBAction)nextAct:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([XMUtil isBlankString:self.nameField.text]) {
        [MBProgressHUD showHUDMsg:@"昵称不能为空!" toView:nil];
        return;
    }
    if ([XMUtil isBlankString:self.ageLa.text] || [self.ageLa.text isEqualToString:@"请选择"]) {
        [MBProgressHUD showHUDMsg:@"年龄不能为空!" toView:nil];
        return;
    }
    
    XMRegistTwoViewController *vc = [[XMRegistTwoViewController alloc] initWithNibName:kStringFromClass(XMRegistTwoViewController) bundle:nil];
    
    vc.gender = self.gender;
    vc.nickname = _nameField.text;
    vc.age = _ageLa.text;
    vc.city = [_addLa.text isEqualToString:@"请选择"] ? @"" : _addLa.text;
    vc.invite_code = _codeField.text;
    vc.content = self.content;
    vc.person_id = self.person_id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)ageAct:(UIButton *)sender {
    [self.view endEditing:YES];
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc] init];
    stringPickerView.pickerMode = BRStringPickerComponentSingle;
    stringPickerView.title = @"选择年龄(岁)";
    stringPickerView.dataSourceArr = [self ageArray];
    if (self.ageLa.text.length > 0 && ![self.ageLa.text isEqualToString:@"--"]) {
        stringPickerView.selectIndex = [[self ageArray] indexOfObject:self.ageLa.text];
    }
    XMWeakSelf(self);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        XMStrongSelf(self);
        self.ageLa.text = resultModel.value;
    };
    stringPickerView.pickerStyle = [self pickerStyle];
    [stringPickerView show];
}

- (IBAction)addAct:(UIButton *)sender {
    [self.view endEditing:YES];
    BRAddressPickerView *addressPickerView = [[BRAddressPickerView alloc] init];
    addressPickerView.pickerMode = BRAddressPickerModeCity;
    addressPickerView.title = @"请选择地区";
    if (self.addLa.text.length > 0 && ![self.addLa.text isEqualToString:@"--"]) {
        addressPickerView.selectValues = [self.addLa.text componentsSeparatedByString:@" "];
    }
    XMWeakSelf(self);
    addressPickerView.resultBlock = ^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
        XMStrongSelf(self);
        self.addLa.text = [NSString stringWithFormat:@"%@ %@", province.name, city.name];
    };
    addressPickerView.pickerStyle = [self pickerStyle];
    [addressPickerView show];
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (textField == self.nameField) {
        if (textField.text.length > kUserNameMaxLength) {
            textField.text = [textField.text substringWithRange:NSMakeRange(0, kUserNameMaxLength)];
        }
    }
    if (textField == self.codeField) {
//        if (textField.text.length > 6) {
//            textField.text = [textField.text substringWithRange:NSMakeRange(0, 6)];
//        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (BRPickerStyle *)pickerStyle {
    BRPickerStyle *customStyle = [[BRPickerStyle alloc] init];
    customStyle.pickerTextColor = kHEX(@"999999");
    customStyle.selectRowTextColor = kAppMainColor;
    customStyle.separatorColor = kAppMainColor;
    customStyle.topCornerRadius = 20;
    customStyle.titleTextFont = kUIFont(16);
    customStyle.titleTextColor = UIColor.blackColor;
    return customStyle;
}
- (NSMutableArray *)ageArray {
    NSMutableArray *ageArray = [NSMutableArray array];
    for (NSInteger age = 18; age <= 70; age++) {
        [ageArray addObject:kStringFormat(@"%ld", age)];
    }
    return ageArray;
}

@end
